package com.github.icloudpay.crm.core.biz;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.icloudpay.crm.core.entity.Consumer;
import com.github.icloudpay.crm.core.entity.ConsumerMessage;
import com.github.icloudpay.crm.core.entity.MerchantAccount;
import com.github.icloudpay.crm.core.entity.MerchantAccountJournalInfo;
import com.github.icloudpay.crm.core.entity.MerchantBase;
import com.github.icloudpay.crm.core.entity.OrderWithdraw;
import com.github.icloudpay.crm.core.entity.PersonalAccount;
import com.github.icloudpay.crm.core.entity.PersonalAccountJournalInfo;
import com.github.icloudpay.crm.core.entity.PersonalBase;
import com.github.icloudpay.crm.core.mapper.ConsumerMapper;
import com.github.icloudpay.crm.core.mapper.ConsumerMessageRelMapper;
import com.github.icloudpay.crm.core.mapper.MerchantAccountJournalMapper;
import com.github.icloudpay.crm.core.mapper.MerchantBaseMapper;
import com.github.icloudpay.crm.core.mapper.OrderWithdrawMapper;
import com.github.icloudpay.crm.core.mapper.PersonalAccountJournalMapper;
import com.github.icloudpay.crm.core.mapper.PersonalBaseMapper;
import com.github.wxiaoqi.security.auth.common.util.CRC16M;
import com.github.wxiaoqi.security.auth.common.util.SnowFlake;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.msg.AccountType;
import com.github.wxiaoqi.security.common.msg.ResponseCode;
import com.github.wxiaoqi.security.common.msg.LoginRole;
import com.github.wxiaoqi.security.common.util.EntityUtils;
import com.github.wxiaoqi.security.common.util.MD5;

/**
 * 注册客户（会员）信息表
 *
 * @author centerroot
 * @email lfw6699@163.com
 * @date 2018-05-25 15:20:48
 */
@Service
public class ConsumerBiz extends BaseBiz<ConsumerMapper,Consumer> {
		private static final Logger logger = LoggerFactory.getLogger(ConsumerBiz.class);
		@Autowired
		private ConsumerMapper consumerMapper;
		@Autowired
		private PersonalBaseMapper personalBaseMapper;
		@Autowired
		private MerchantBaseMapper merchantBaseMapper;
		@Autowired
		private ConsumerMessageRelMapper consumerMessageRelMapper;
		@Autowired
		private PersonalAccountJournalMapper personalAccountJournalMapper;
		@Autowired
		private MerchantAccountJournalMapper merchantAccountJournalMapper;
		@Autowired
		private PersonalBaseBiz personalBaseBiz;
		@Autowired
		private MerchantBaseBiz merchantBaseBiz;
		@Autowired
		private PersonalAccountBiz personalAccountBiz;
		@Autowired
		private MerchantAccountBiz merchantAccountBiz;
		@Autowired
		private OrderWithdrawMapper orderWithdrawMapper;
		@Autowired
		private SmsGateWayBiz smsGateWayBiz;
		@Value("${bountyHunter.acceptBizNo}")
		private String acceptBizNo;
		
		/**
		 * 获取用户统计数据
		 * @author centerroot
		 * @time 创建时间:2018年5月23日上午11:31:05
		 * @param param
		 * @return
		 */
		public Map<String, Object> getConsumerStatistics(Map<String, Object> param){
			logger.info("获取用户统计数据   请求参数：{}",param);
			Map<String, Object> respMap = new HashMap<String, Object>();
			String consumerId = (String) param.get("consumerId");
			Consumer consumer = consumerMapper.selectByPrimaryKey(consumerId);
			if (null != consumer) {
				if (null != consumer.getPerId()) {
					Map<String, Object> reqCustMap = new HashMap<String, Object>();
					reqCustMap.put("perId", consumer.getPerId());
					Map<String, Object> respCustMap = personalBaseBiz.getCustStatistics(reqCustMap);
					if (ResponseCode.OK.getCode().equals(respCustMap.get("code"))) {
						respCustMap.remove("code");
						respCustMap.remove("msg");
						respMap.put("custStatistics", respCustMap);
					} else {
						respMap.put("custStatistics", "");
					}
				}else{
					respMap.put("custStatistics", "");
				}
				if (null != consumer.getMerId()) {
					Map<String, Object> reqMerMap = new HashMap<String, Object>();
					reqMerMap.put("merId", consumer.getMerId());
					Map<String, Object> respMerMap = merchantBaseBiz.getMerStatistics(reqMerMap);
					if (ResponseCode.OK.getCode().equals(respMerMap.get("code"))) {
						respMerMap.remove("code");
						respMerMap.remove("msg");
						respMap.put("merStatistics", respMerMap);
					} else {
						respMap.put("merStatistics", "");
					}
				}else{
					respMap.put("merStatistics", "");
				}
				respMap.put("code", ResponseCode.OK.getCode());
				respMap.put("msg", ResponseCode.OK.getMessage());
			}else{
				respMap.put("code", ResponseCode.USER_NOTEXIST.getCode());
				respMap.put("msg", ResponseCode.USER_NOTEXIST.getMessage());
			}
			logger.info("获取用户统计数据   响应参数：{}",respMap);
			return respMap;
		}
		
		/**
		 * 用户基本信息录入
		 * @author centerroot
		 * @time 创建时间:2018年5月23日下午1:43:32
		 * @param param
		 * @return
		 */
		public Map<String, Object> savePersonalBaseInfo(Map<String, Object> param){
			logger.info("用户基本信息录入   请求参数：{}",param);
			Map<String, Object> respMap = new HashMap<String, Object>();
			String consumerId = (String) param.get("consumerId");
			// 信息类型 00：会员信息  01： 商户信息
			String infoType = (String) param.get("infoType");
			Consumer consumer = consumerMapper.selectByPrimaryKey(consumerId);
			if (null != consumer) {
				if ("00".equals(infoType)) {
					if (null != consumer.getPerId()) {
						param.put("perId", consumer.getPerId());
						personalBaseBiz.saveCustBaseInfo(param);
						respMap.put("code", ResponseCode.OK.getCode());
						respMap.put("msg", ResponseCode.OK.getMessage());
					}else{
						respMap.put("code", ResponseCode.CUST_NOTEXIST.getCode());
						respMap.put("msg", ResponseCode.CUST_NOTEXIST.getMessage());
					}
				} else {
					if (null != consumer.getMerId()) {
						// TODO 商户信息保存
						
						respMap.put("code", ResponseCode.OK.getCode());
						respMap.put("msg", ResponseCode.OK.getMessage());
					}else{
						respMap.put("code", ResponseCode.MER_NOTEXIST.getCode());
						respMap.put("msg", ResponseCode.MER_NOTEXIST.getMessage());
					}
				}
				
			}else{
				respMap.put("code", ResponseCode.USER_NOTEXIST.getCode());
				respMap.put("msg", ResponseCode.USER_NOTEXIST.getMessage());
			}
			logger.info("用户基本信息录入   响应参数：{}",respMap);
			return respMap;
		}
		
		/**
		 * 用户实名认证信息录入
		 * @author centerroot
		 * @time 创建时间:2018年5月23日下午2:46:27
		 * @param param
		 * @return
		 */
		public Map<String, Object> verifiedRealName(Map<String, Object> param){
			logger.info("用户实名认证信息录入   请求参数：{}",param);
			Map<String, Object> respMap = new HashMap<String, Object>();
			String consumerId = (String) param.get("consumerId");
			// 信息类型 00：会员信息  01： 商户信息
			String infoType = (String) param.get("infoType");
			Consumer consumer = consumerMapper.selectByPrimaryKey(consumerId);
			if (null != consumer) {
				if ("00".equals(infoType)) {
					if (null != consumer.getPerId()) {
						param.put("perId", consumer.getPerId());
						personalBaseBiz.verifiedRealName(param);
						
						respMap.put("code", ResponseCode.OK.getCode());
						respMap.put("msg", ResponseCode.OK.getMessage());
					}else{
						respMap.put("code", ResponseCode.CUST_NOTEXIST.getCode());
						respMap.put("msg", ResponseCode.CUST_NOTEXIST.getMessage());
					}
				} else {
					if (null != consumer.getMerId()) {
						// TODO 商户信息保存
						
						respMap.put("code", ResponseCode.OK.getCode());
						respMap.put("msg", ResponseCode.OK.getMessage());
					}else{
						respMap.put("code", ResponseCode.MER_NOTEXIST.getCode());
						respMap.put("msg", ResponseCode.MER_NOTEXIST.getMessage());
					}
				}
				
			}else{
				respMap.put("code", ResponseCode.USER_NOTEXIST.getCode());
				respMap.put("msg", ResponseCode.USER_NOTEXIST.getMessage());
			}
			logger.info("用户实名认证信息录入   响应参数：{}",respMap);
			return respMap;
		}
		
		/**
		 * 用户安全信息查询
		 * @author centerroot
		 * @time 创建时间:2018年5月23日下午2:00:35
		 * @param param
		 * @return
		 */
		public Map<String, Object> getSecurityInfo(Map<String, Object> param){
			logger.info("用户安全信息查询   请求参数：{}",param);
			Map<String, Object> respMap = new HashMap<String, Object>();
			String consumerId = (String) param.get("consumerId");
			Consumer consumer = consumerMapper.selectByPrimaryKey(consumerId);
			if (null != consumer) {
				if (null != consumer.getPerId()) {
					PersonalBase personalBase = personalBaseMapper.selectByPrimaryKey(consumer.getPerId());
					respMap.put("custVerifyFlag", personalBase.getVerifyFlag());
				}else{
					respMap.put("custVerifyFlag", "");
				}
				if (null != consumer.getMerId()) {
					MerchantBase merchantBase = merchantBaseMapper.selectByPrimaryKey(consumer.getMerId());
					respMap.put("merVerifyFlag", merchantBase.getVerifyFlag());
				}else{
					respMap.put("merVerifyFlag", "");
				}
				
				respMap.put("mobile", consumer.getMobile());
				respMap.put("payPWDStatus", consumer.getPayPasswordStatus());
				respMap.put("code", ResponseCode.OK.getCode());
				respMap.put("msg", ResponseCode.OK.getMessage());
			}else{
				respMap.put("code", ResponseCode.USER_NOTEXIST.getCode());
				respMap.put("msg", ResponseCode.USER_NOTEXIST.getMessage());
			}
			logger.info("用户安全信息查询   响应参数：{}",respMap);
			return respMap;
		}
		
		/**
		 * 修改手机号
		 * 1、判断用户是否存在
		 * 2、判断用户原手机号是否正确
		 * 3、判断原手机号和原验证码正确性
		 * 4、判断新手机号和新验证码正确性
		 * 5、更换手机号
		 * @author centerroot
		 * @time 创建时间:2018年5月23日下午2:24:30
		 * @param param
		 * @return
		 */
		public Map<String, Object> modifyConsumerMobile(Map<String, Object> param){
			logger.info("修改手机号   请求参数：{}",param);
			Map<String, Object> respMap = new HashMap<String, Object>();
			String oldMobile = (String) param.get("oldMobile");
			String oldSmsCode = (String) param.get("oldSmsCode");
			String newMobile = (String) param.get("newMobile");
			String newSmsCode = (String) param.get("newSmsCode");
			String consumerId = (String) param.get("consumerId");
			Consumer consumer = consumerMapper.selectByPrimaryKey(consumerId);
			if (null != consumer) {
				if (oldMobile.equals(consumer.getMobile())) {
					Map<String, Object> smsMap = new HashMap<String, Object>();
					smsMap.put("phone", oldMobile);
					smsMap.put("code", oldSmsCode);
					Map<String, Object> smsMapResp = smsGateWayBiz.verificationCode(param);
					// 判断原手机号和原验证码正确性
					if (ResponseCode.OK.getCode().equals(smsMapResp.get("code")) && (Boolean)smsMapResp.get("verifyStatus")) {
						smsMap.put("phone", newMobile);
						smsMap.put("code", newSmsCode);
						smsMapResp = smsGateWayBiz.verificationCode(param);
						// 判断新手机号和新验证码正确性
						if (ResponseCode.OK.getCode().equals(smsMapResp.get("code")) && (Boolean)smsMapResp.get("verifyStatus")) {
							Consumer checkConsumer=consumerMapper.queryUserinfoByMobile(newMobile);
							if (null == checkConsumer) {
								consumer.setMobile(newMobile);
								consumer.setUpdatetime(new Timestamp(System.currentTimeMillis()));
								consumerMapper.updateByPrimaryKey(consumer);
								
								respMap.put("code", ResponseCode.OK.getCode());
								respMap.put("msg", ResponseCode.OK.getMessage());
							}else{
								respMap.put("code", ResponseCode.MOBILE_REPEAT.getCode());
								respMap.put("msg", ResponseCode.MOBILE_REPEAT.getMessage());
							}
							
						} else {
							respMap.put("code", ResponseCode.SMS_CODE_MATCH_FAIL.getCode());
							respMap.put("msg", ResponseCode.SMS_CODE_MATCH_FAIL.getMessage());
						}
					} else {
						respMap.put("code", ResponseCode.SMS_CODE_MATCH_FAIL.getCode());
						respMap.put("msg", ResponseCode.SMS_CODE_MATCH_FAIL.getMessage());
					}
					
				} else {
					respMap.put("code", ResponseCode.MOBILE_MATCH_FAIL.getCode());
					respMap.put("msg", ResponseCode.MOBILE_MATCH_FAIL.getMessage());
				}
			}else{
				respMap.put("code", ResponseCode.USER_NOTEXIST.getCode());
				respMap.put("msg", ResponseCode.USER_NOTEXIST.getMessage());
			}
			logger.info("修改手机号   响应参数：{}",respMap);
			return respMap;
		}
		
		
		//根据手机号获取登录用户信息,并校验用户密码是否正确
		public Map<String, Object> queryConsumerinfoByMobile(String mobile,String passeword){
			Map<String, Object> respMap = new HashMap<String, Object>();
			try {
				Consumer consumer=consumerMapper.queryUserinfoByMobile(mobile);
				if(null!=consumer){
					respMap=EntityUtils.beanToMap(consumer);
					String inputpassword=MD5.sign(passeword, consumer.getSaltvalue(), "utf-8");
					if(consumer.getPassword().equals(inputpassword)){
						//修改用户登录信息
						Consumer newconsumer = new Consumer();
						newconsumer.setConsumerId(consumer.getConsumerId());
						newconsumer.setLastLoginTime(new Timestamp(System.currentTimeMillis()));
						consumerMapper.updateByPrimaryKeySelective(newconsumer);
						respMap.put("code", ResponseCode.OK.getCode());
						respMap.put("msg", ResponseCode.OK.getMessage());
					}else{
						respMap.put("code", ResponseCode.USERINFO_PWD_ERROR.getCode());
						respMap.put("msg", ResponseCode.USERINFO_PWD_ERROR.getMessage());
					}
				}else{
					respMap.put("code", ResponseCode.USERINFO_NOTEXIST.getCode());
					respMap.put("msg", ResponseCode.USERINFO_NOTEXIST.getMessage());
				}
			} catch (Exception e) {
				respMap.put("code", ResponseCode.USERINFO_NOTEXIST.getCode());
				respMap.put("msg", ResponseCode.USERINFO_NOTEXIST.getMessage());
				e.printStackTrace();
				logger.info("根据手机号获取登录用户信息报错：",e.getMessage());
			}
			return respMap;
		}
		
		/**
		 * 查询用户消息列表
		 * @author centerroot
		 * @time 创建时间:2018年5月23日下午4:53:21
		 * @param param
		 * @return
		 */
		public Map<String, Object> getConsumerMessage(Map<String, Object> param){
			logger.info("查询用户消息列表   请求参数：{}",param);
			Map<String, Object> respMap = new HashMap<String, Object>();
			String consumerId = (String) param.get("consumerId");
			Consumer consumer = consumerMapper.selectByPrimaryKey(consumerId);
			if (null != consumer) {
				List<ConsumerMessage> userMessages = consumerMessageRelMapper.queryUserMessage(param);
				
				respMap.put("userMessages", userMessages);
				respMap.put("code", ResponseCode.OK.getCode());
				respMap.put("msg", ResponseCode.OK.getMessage());
			}else{
				respMap.put("code", ResponseCode.USER_NOTEXIST.getCode());
				respMap.put("msg", ResponseCode.USER_NOTEXIST.getMessage());
			}
			logger.info("查询用户消息列表   响应参数：{}",respMap);
			return respMap;
		}
		
		/**
		 * 修改用户消息状态
		 * @author centerroot
		 * @time 创建时间:2018年5月23日下午5:06:04
		 * @param param
		 * @return
		 */
		public Map<String, Object> updateConsumerMessage(Map<String, Object> param){
			logger.info("修改用户消息状态   请求参数：{}",param);
			Map<String, Object> respMap = new HashMap<String, Object>();
			String consumerId = (String) param.get("consumerId");
			Consumer consumer = consumerMapper.selectByPrimaryKey(consumerId);
			if (null != consumer) {
				consumerMessageRelMapper.updateUserMessage(param);
				
				respMap.put("code", ResponseCode.OK.getCode());
				respMap.put("msg", ResponseCode.OK.getMessage());
			}else{
				respMap.put("code", ResponseCode.USER_NOTEXIST.getCode());
				respMap.put("msg", ResponseCode.USER_NOTEXIST.getMessage());
			}
			logger.info("修改用户消息状态   响应参数：{}",respMap);
			return respMap;
		}

		/**
		 * 查询用户最后收入记录
		 * @author centerroot
		 * @time 创建时间:2018年5月24日上午9:21:45
		 * @param param
		 * @return
		 */
		public Map<String, Object> getLastIncomeRecord(Map<String, Object> param){
			logger.info("查询用户最后收入记录   请求参数：{}",param);
			Map<String, Object> respMap = new HashMap<String, Object>();
			String consumerId = (String) param.get("consumerId");
			Consumer consumer = consumerMapper.selectByPrimaryKey(consumerId);
			if (null != consumer) {
				if (null != consumer.getPerId()) {
					PersonalAccount reqPersonalAccount = new PersonalAccount();
					reqPersonalAccount.setPerId(consumer.getPerId());
					reqPersonalAccount.setAccType(AccountType.ACC_BALANCE.getCode());
					PersonalAccount respPersonalAccount = personalAccountBiz.selectOne(reqPersonalAccount);
					if (null != respPersonalAccount) {
						Map<String, Object> reqMap = new HashMap<String, Object>();
						reqMap.put("perId", consumer.getPerId());
						reqMap.put("inOutFlag", "1");
						reqMap.put("accType", AccountType.ACC_BALANCE.getCode());
						reqMap.put("limitNum", "5");
						List<PersonalAccountJournalInfo> personalAccountJournals = personalAccountJournalMapper.queryTransactionRecord(reqMap);
						
						respMap.put("accBalance", respPersonalAccount.getAccBalance());
						respMap.put("personalAccountJournals", personalAccountJournals);
						respMap.put("code", ResponseCode.OK.getCode());
						respMap.put("msg", ResponseCode.OK.getMessage());
					} else {
						respMap.put("code", ResponseCode.CUST_ACC_NOTEXIST.getCode());
						respMap.put("msg", ResponseCode.CUST_ACC_NOTEXIST.getMessage());
					}
				} else {
					respMap.put("code", ResponseCode.CUST_NOTEXIST.getCode());
					respMap.put("msg", ResponseCode.CUST_NOTEXIST.getMessage());
				}
			}else{
				respMap.put("code", ResponseCode.USER_NOTEXIST.getCode());
				respMap.put("msg", ResponseCode.USER_NOTEXIST.getMessage());
			}
			logger.info("查询用户最后收入记录   响应参数：{}",respMap);
			return respMap;
		}
		
		/**
		 * 查询用户交易记录
		 * @author centerroot
		 * @time 创建时间:2018年5月24日上午9:21:45
		 * @param param
		 * @return
		 */
		public Map<String, Object> queryPersonaTransactionRecord(Map<String, Object> param){
			logger.info("查询用户交易记录   请求参数：{}",param);
			Map<String, Object> respMap = new HashMap<String, Object>();
			String consumerId = (String) param.get("consumerId");
			Consumer consumer = consumerMapper.selectByPrimaryKey(consumerId);
			if (null != consumer) {
				if (null != consumer.getPerId()) {
					PersonalAccount reqPersonalAccount = new PersonalAccount();
					reqPersonalAccount.setPerId(consumer.getPerId());
					reqPersonalAccount.setAccType(AccountType.ACC_BALANCE.getCode());
					PersonalAccount respPersonalAccount = personalAccountBiz.selectOne(reqPersonalAccount);
					if (null != respPersonalAccount) {
						param.put("perId", consumer.getPerId());
						param.put("accType", AccountType.ACC_BALANCE.getCode());
						List<PersonalAccountJournalInfo> personalAccountJournals = personalAccountJournalMapper.queryTransactionRecord(param);
						
						respMap.put("accBalance", respPersonalAccount.getAccBalance());
						respMap.put("personalAccountJournals", personalAccountJournals);
						respMap.put("code", ResponseCode.OK.getCode());
						respMap.put("msg", ResponseCode.OK.getMessage());
					} else {
						respMap.put("code", ResponseCode.CUST_ACC_NOTEXIST.getCode());
						respMap.put("msg", ResponseCode.CUST_ACC_NOTEXIST.getMessage());
					}
				} else {
					respMap.put("code", ResponseCode.CUST_NOTEXIST.getCode());
					respMap.put("msg", ResponseCode.CUST_NOTEXIST.getMessage());
				}
			}else{
				respMap.put("code", ResponseCode.USER_NOTEXIST.getCode());
				respMap.put("msg", ResponseCode.USER_NOTEXIST.getMessage());
			}
			logger.info("查询用交易记录   响应参数：{}",respMap);
			return respMap;
		}
		
		/**
		 * 查询用户提现记录
		 * @author centerroot
		 * @time 创建时间:2018年5月24日上午11:48:38
		 * @param param
		 * @return
		 */
		public Map<String, Object> queryWithDrawRecord(Map<String, Object> param){
			logger.info("查询用户提现记录   请求参数：{}",param);
			Map<String, Object> respMap = new HashMap<String, Object>();
			String consumerId = (String) param.get("consumerId");
			Consumer consumer = consumerMapper.selectByPrimaryKey(consumerId);
			if (null != consumer) {
				if (null != consumer.getPerId()) {
					List<OrderWithdraw> custWithdraws = orderWithdrawMapper.queryWithDrawRecord(param);
					
					respMap.put("custWithdraws", custWithdraws);
					respMap.put("code", ResponseCode.OK.getCode());
					respMap.put("msg", ResponseCode.OK.getMessage());
				} else {
					respMap.put("code", ResponseCode.CUST_NOTEXIST.getCode());
					respMap.put("msg", ResponseCode.CUST_NOTEXIST.getMessage());
				}
			}else{
				respMap.put("code", ResponseCode.USER_NOTEXIST.getCode());
				respMap.put("msg", ResponseCode.USER_NOTEXIST.getMessage());
			}
			logger.info("查询用户提现记录   响应参数：{}",respMap);
			return respMap;
		}
		
		/**
		 * 查询商户交易记录
		 * @author centerroot
		 * @time 创建时间:2018年5月28日下午1:51:20
		 * @param param
		 * @return
		 */
		public Map<String, Object> queryMerchantTransactionRecord(Map<String, Object> param){
			logger.info("查询商户交易记录   请求参数：{}",param);
			Map<String, Object> respMap = new HashMap<String, Object>();
			String consumerId = (String) param.get("consumerId");
			Consumer consumer = consumerMapper.selectByPrimaryKey(consumerId);
			if (null != consumer) {
				if (null != consumer.getMerId()) {
					MerchantAccount reqMerchantAccount = new MerchantAccount();
					reqMerchantAccount.setMerId(consumer.getMerId());
					reqMerchantAccount.setAccType(AccountType.ACC_BALANCE.getCode());
					MerchantAccount respMerchantAccount = merchantAccountBiz.selectOne(reqMerchantAccount);
					
					
					MerchantAccount reqMerAccountFreeze = new MerchantAccount();
					reqMerAccountFreeze.setMerId(consumer.getMerId());
					reqMerAccountFreeze.setAccType(AccountType.ACC_BALANCE.getCode());
					MerchantAccount respMerAccountFreeze = merchantAccountBiz.selectOne(reqMerAccountFreeze);
					if (null != respMerchantAccount) {
						param.put("perId", consumer.getPerId());
						param.put("accType", AccountType.ACC_BALANCE.getCode());
						List<MerchantAccountJournalInfo> merchantAccountJournalInfos = merchantAccountJournalMapper.queryTransactionRecord(param);
						
						
						if (null != respMerAccountFreeze) {
							respMap.put("accFreezeBalance", respMerAccountFreeze.getAccBalance());
						}
						respMap.put("accBalance", respMerchantAccount.getAccBalance());
						respMap.put("merchantAccountJournalInfos", merchantAccountJournalInfos);
						respMap.put("code", ResponseCode.OK.getCode());
						respMap.put("msg", ResponseCode.OK.getMessage());
					} else {
						respMap.put("code", ResponseCode.MER_ACC_NOTEXIST.getCode());
						respMap.put("msg", ResponseCode.MER_ACC_NOTEXIST.getMessage());
					}
				} else {
					respMap.put("code", ResponseCode.MER_NOTEXIST.getCode());
					respMap.put("msg", ResponseCode.MER_NOTEXIST.getMessage());
				}
			}else{
				respMap.put("code", ResponseCode.USER_NOTEXIST.getCode());
				respMap.put("msg", ResponseCode.USER_NOTEXIST.getMessage());
			}
			logger.info("查询商户交易记录   响应参数：{}",respMap);
			return respMap;
		}
		
		/**
		 * 查询会员列表
		 * @author chj
		 * @param param
		 * @return
		 */
		public Map<String, Object> queryMemberList(Map<String, Object> param) {
			logger.info("查询会员列表参数。。。{}"+param);
			Map<String, Object> respMap = new HashMap<String, Object>();
			List<Map<String, Object>> res = consumerMapper.queryMemberList(param);
			logger.info("查询会员列表。。。{}"+res);
			respMap.put("memberList", res);
			return respMap;
		}
		
		/**
		 * 查询会员信息
		 * @author chj
		 * @param param
		 * @return
		 */
		public Map<String, Object> queryMemberInfo(Map<String, Object> param) {
			logger.info("查询会员信息参数。。。{}"+param);
			Map<String, Object> respMap = new HashMap<String, Object>();
			Map<String, Object> res = consumerMapper.queryMemberInfo(param);
			logger.info("查询会员信息。。。{}"+res);
			respMap.put("memberInfo", res);
			return respMap;
		}
		
		/**
		 * 修改会员信息
		 * @author chj
		 * @param param
		 * @return
		 */
		public Map<String, Object> updateAccountStatus(Map<String, Object> param) {
			logger.info("修改会员状态参数。。。{}"+param);
			Map<String, Object> respMap = new HashMap<String, Object>();
			consumerMapper.updateAccountStatus(param);
			logger.info("修改会员状态完成。。。{}");
			respMap.put("result", "修改成功");
			return respMap;
		}
		
		
		/**
		 * 查询实名记录
		 * @param param
		 * @return
		 */
		public Map<String, Object> queryRealNameRecord(Map<String, Object> param) {
			logger.info("查询实名认证记录参数。。。{}"+param);
			Map<String, Object> respMap = new HashMap<String, Object>();
			List<Map<String, Object>> res = consumerMapper.queryRealNameRecord();
			logger.info("查询实名认证记录。。。{}"+res);
			respMap.put("realRecord", res);
			return respMap;
		}
		
		/**
		 * 修改实名状态
		 * @param param
		 * @return
		 */
		public Map<String, Object> updateVerifyFlag(Map<String, Object> param) {
			logger.info("修改实名状态参数。。。{}"+param);
			Map<String, Object> respMap = new HashMap<String, Object>();
			consumerMapper.updateVerifyFlag(param);
			logger.info("修改实名状态完成。。。{}");
			respMap.put("result", "成功");
			return respMap;
		}
		
		/**
		 * 用户注册
		 * @param 
		 * 
		 * 
		 * */
		public Map<String, Object> userRegister(Map<String, Object> param){
			logger.info("用户信息注册   请求参数：{}",param);
			Map<String, Object> respMap = new HashMap<String, Object>();
			String mobile=(String) param.get("mobile");
			String loginRole=(String) param.get("loginrole");//登录权限
			Consumer registerUser=consumerMapper.queryUserinfoByMobile(mobile);
			if(null!=registerUser){
				respMap.put("code", ResponseCode.USERINFO_ISEXIST.getCode());
				respMap.put("msg", ResponseCode.USERINFO_ISEXIST.getMessage());
				return respMap;
			}
			Consumer consumer= new Consumer();
			String consumerId =String.valueOf(SnowFlake.getId());
			//获取用户随机盐值
			String saltvalue=MD5.generateSaltkey();
			String password=MD5.sign((String) param.get("password"),saltvalue, "utf-8");
			String platformId=(String) param.get("platformId");
			try {
				String perId="";
				String merId="";
				if(LoginRole.personal.getCode().equals(loginRole)){//个人登录
					perId= String.valueOf(SnowFlake.getId());
				}else if(LoginRole.merchant.getCode().equals(loginRole)){//商户登录
					merId= String.valueOf(SnowFlake.getId());
				}
				consumer.setConsumerId(consumerId);
				consumer.setPlatformId(platformId);
				consumer.setMobile(mobile);
				consumer.setPassword(password);
				consumer.setPasswordStatus("00");//正常
				consumer.setPerId(perId);
				consumer.setMerId(merId);
				consumer.setSaltvalue(saltvalue);
				consumer.setAccountStatus("00");
				consumer.setLastLoginRole(loginRole);
				consumer.setLastLoginTime(new Timestamp(System.currentTimeMillis()));
				consumer.setCreatetime(new Timestamp(System.currentTimeMillis()));
				int isInsertOK=consumerMapper.insertSelective(consumer);
				if(isInsertOK>0){
					//添加商户基础信息
					/*if(!StringUtils.isEmpty(perId)){//添加个人基础信息 
						PersonalBase personalBase=new PersonalBase();
						//生成个人会员号
						String customerIdStr = String.valueOf(SnowFlake.getId());
						String perNo = CRC16M.getCRCNo(customerIdStr);
						personalBase.setPerId(perId);
						personalBase.setPerNo(perNo);
						personalBase.setConsumerId(consumerId);
						personalBase.setCreatetime(new Timestamp(System.currentTimeMillis()));
						personalBase.setUpdatetime(new Timestamp(System.currentTimeMillis()));
						personalBaseMapper.insertSelective(personalBase);
					}
					//添加商户基础信息
					if(!StringUtils.isEmpty(merId)){
						MerchantBase merchantBase =new MerchantBase();
						//生成商户会员号
						String customerIdStr = String.valueOf(SnowFlake.getId());
						String merchantNo = CRC16M.getCRCNo(customerIdStr);
						merchantBase.setMerId(merId);
						merchantBase.setMerchantNo(merchantNo);
						merchantBase.setConsumerId(consumerId);
						merchantBase.setCreatetime(new Timestamp(System.currentTimeMillis()));
						merchantBase.setUpdatetime(new Timestamp(System.currentTimeMillis()));
						merchantBaseMapper.insertSelective(merchantBase);
					}*/
					respMap.put("code", ResponseCode.OK.getCode());
					respMap.put("msg", ResponseCode.OK.getMessage());
				}
				respMap.put("code", ResponseCode.REGISTER_ERROR.getCode());
				respMap.put("msg", ResponseCode.REGISTER_ERROR.getMessage());
			} catch (Exception e) {
				e.printStackTrace();
				respMap.put("code", ResponseCode.REGISTER_ERROR.getCode());
				respMap.put("msg", ResponseCode.REGISTER_ERROR.getMessage());
				return respMap;
			}
			return respMap;
		}
				
}